﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using sqengine.Components;
#endregion


namespace sqengine.PhysicObjects
{
    public class ItmWebBox : MovableObject
    {

        private int browser_id = -1;
        private InputBox webaddress_input;

        public ItmWebBox(Game game, string ObjName, Model model, Matrix orientation, float ObjScale, Vector3 position)
            : base(game, ObjName, model, orientation, ObjScale, position, true)
        {
            ActionDistance = -1f;

            IsCustomEffect = false;


            OnAction += new ActionHandler(ItmWebBox_OnAction);
            OnMouseOver += new MouseEventHandler(ItmWebBox_OnMouseOver);
            OnMouseLeave += new MouseEventHandler(ItmWebBox_OnMouseLeave);

            webaddress_input = new InputBox(game, Adcontent, ((sqengine)game).Keyman, "Fonts/Visitor");
            webaddress_input.OnClose += new InputBox.InputHandler(webaddress_input_OnClose);
            webaddress_input.DrawOrder = 1;
            CurrentGame.Components.Add(webaddress_input);

            browser_id = CurrentWebBrowserPool.AddWebWindow(800, 600);

            CurrentWebBrowserPool.AttachReceiveTitle(browser_id, new CjcAwesomiumWrapper.WebViewListener.ReceiveTitleDelegate(weblist_ReceiveTitle));

            if (browser_id != -1)
                CurrentWebBrowserPool.Navigate(browser_id, "http://www.youtube.com");
        }

        void webaddress_input_OnClose(object sender, InputBox.InputArg ca)
        {
            if (ca.Input().Length > 0)
                CurrentWebBrowserPool.Navigate(browser_id, ca.Input());
        }

        void ItmWebBox_OnMouseLeave(object send, MouseArg e)
        {
            CurrentGame.Window.Title = "Mouse leave";
        }

        void ItmWebBox_OnMouseOver(object send, MouseArg e)
        {
            int w = 0;
            int h = 0;

            if (ObjectTexture != null)
            {
                w = (int)(Math.Abs(e.Position().X) * ObjectTexture.Width / 16);
                h = (int)(Math.Abs(e.Position().Y) * ObjectTexture.Height / 16);
            }

            if (body.CollisionSkin.GetPrimitiveLocal(0) != null)
            {
                Box Test = (Box)body.CollisionSkin.GetPrimitiveLocal(0);
                Vector3 point;
                Test.GetDistanceToPoint(out point, e.Position());
                CurrentGame.Window.Title = point.ToString();
            }
            CurrentWebBrowserPool.InjectMouseMove(browser_id, w, h);

            //CurrentGame.Window.Title = string.Format("x {0} y {1}", w, h);
        }

        void ItmWebBox_OnAction(object sender, ActionArg ca)
        {
            if (!webaddress_input.IsVisible)
                webaddress_input.Show(-1, -1);
        }

        public override void ApplyEffects(BasicEffect effect)
        {
            if (CurrentWebBrowserPool.GetRendererWebpage(browser_id) != null)
            {
              
                effect.TextureEnabled = true;
                effect.Texture = CurrentWebBrowserPool.GetRendererWebpage(browser_id);
            }


        }

        public override void Update(GameTime gameTime)
        {

            

            if (CurrentWebBrowserPool.GetRendererWebpage(browser_id) != null)
            {

                ObjectTexture = CurrentWebBrowserPool.GetRendererWebpage(browser_id);

            }

            

            base.Update(gameTime);

        }

        void weblist_ReceiveTitle(string title, string frameName)
        {
            //((sqengine)CurrentGame).AddMessage("Title recived", Color.Blue, 2000, string.Empty);


        }
    }
}
